package demo1;

import java.util.ArrayList;

public class Test5 {
    public static void main(String[] args) {
        String s = "dBowNgxyPZXZXlBTMKFskExQEK";
        int[] upCase = new int[26];
        int[] loCase = new int[26];
        ArrayList<Character> arrayList = new ArrayList<>();
        int i = 0;
        while (i < s.length()) {
            arrayList.add(s.charAt(i++));
        }
        for (int j = 0; j < s.length(); j++) {
            if (isCase(s.charAt(j)) == 1) {
                loCase[s.charAt(j) - 97]++;
            } else {
                upCase[s.charAt(j) - 65]++;
            }
        }
        for (int j = 0; j < s.length(); j++) {
            if (isCase(s.charAt(j)) == 1) {
                if (loCase[s.charAt(j) - 97] >= 2) {
                    int del = 1;
                    while (del++ < loCase[s.charAt(j) - 97]) {
                        int lastindex = arrayList.lastIndexOf(s.charAt(j));
                        arrayList.remove(lastindex);
                    }
                    loCase[s.charAt(j) - 97] = 1;
                }
            } else {
                if (upCase[s.charAt(j) - 65] >= 2) {
                    int del = 1;
                    while (del++ < upCase[s.charAt(j) - 65]) {
                        int lastindex = arrayList.lastIndexOf(s.charAt(j));
                        arrayList.remove(lastindex);
                    }
                    upCase[s.charAt(j) - 65] = 1;
                }
            }
        }
        char[] chars = new char[arrayList.size()];
        for (int j = 0; j < arrayList.size(); j++) {
            chars[j] = arrayList.get(j);
        }
        System.out.println(new String(chars));
    }
    public static int isCase(char ch) {
        if (ch >= 'a' && ch <= 'z') {
            return 1;
        }
        return -1;
    }
}
